/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package num;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import org.ntu.csn.as.manager.DBManager;

/**
 *
 * @author hongjun
 */
public class VarManager {  
    //the base template
    protected String in_ques_tpl = "Enter Your Original Math Question Here.";
    //the default template
    protected String dft_ques_tpl = in_ques_tpl;
    protected String tpquestion = "No Question Generated";
    protected ArrayList<String> RelList = new ArrayList<String>();
    protected ArrayList<String> VarList = new ArrayList<String>();    
    protected ArrayList<String> SolList = new ArrayList<String>();
    protected ArrayList<String> TplList = new ArrayList<String>();
    
    protected ArrayList<Integer> ValList = new ArrayList<Integer>();
    
    protected ArrayList<String[]> TypNamList = new ArrayList<String[]>();
    protected ArrayList<String> PhsNamList = new ArrayList<String>(); //original phrase name list   
    protected ArrayList<String> rPhsNamList = new ArrayList<String>();//randomized phrase name list
    ArrayList<Integer[]> TypNumList = new ArrayList<Integer[]>();
    
    protected Tokenizer tok=new Tokenizer();    
    protected String hint = "no hint";
        //hj: she after he, she is 1 3 5 he is 0 2 4 6
    protected static String[] englishHeShe = {
        "he", "she", "his", "her", "him", "her", "himself", "herself"
    };
    private int max_iter=1000;
    private int iter=0;
    
    VarManager(){
    }
 
    VarManager(ArrayList<String> VarList,ArrayList<String> RelList,ArrayList<String> SolList,ArrayList<String> TplList){
        this();
        this.VarList = VarList;
        this.RelList = RelList;
        this.SolList = SolList;
        this.TplList = TplList;
    }

    public ArrayList<Integer> getrValList(int iter_) throws SQLException{
 
        String test;
        this.iter=iter_;
        ValList.clear();
        for(int i=0;i<VarList.size();i++){
            ValList.add(tok.getRandom(VarList.get(i)));
        }
        if (iter++>max_iter){ 
            hint="Max Iteration Reached, Unable to Find a Question";
            return ValList; 
        }
        for(int i=0;i<RelList.size();i++){
            test = RelList.get(i);
            for(int j=0;j<VarList.size();j++){
            test=test.replace(tok.getVariable(VarList.get(j)), ValList.get(j).toString());
            }
            if (Integer.valueOf(DBManager.doCalculation(test))==0)
                
                    getrValList(iter);
               
        }
        return ValList;
    }
    
    public ArrayList<String> getrPhsNamList(int iter_) throws SQLException{
 
        rPhsNamList.clear();
        int i=0;
        int ite=0;    
        
        while( rPhsNamList.size()<TypNumList.size()){
            ite++;
            int a=TypNumList.get(i)[0];
            if (ite >= max_iter ){ //|| DBManager.getDictSize(a) <2*TypNumList.size()
                Collections.copy(rPhsNamList, PhsNamList);
                return rPhsNamList;
            }
            String subname=DBManager.getRandomName(a);
                //if (!PhsNamList.contains(subname) && !rPhsNamList.contains(subname)){
               if (!PhsNamList.contains(subname) && !rPhsNamList.contains(subname)){
               rPhsNamList.add(subname);
               i++;
            }
        }
        return rPhsNamList;
    }
    

    public static void main(String args[]) throws SQLException{
        VarManager vm = new VarManager();
        vm.VarList.add("var_int1:1:2:10:isNumber");
        vm.VarList.add("var_int2:1:2:10:isTimes");
        vm.VarList.add("var_int3:1:2:10:isTimes");
        vm.RelList.add("var_int1>var_int2+var_int3");
        String test="this is a man and another Man's and a human";
        String old="Man";
        String ans = test.replaceAll("\\s+", "");
        
//        ans = replaceAllWords(test,old, "woman");
         for (int j=0; j<10;j++){
             vm.getrValList(0); 
        for (int i=0; i<vm.ValList.size();i++){
            System.out.print(vm.ValList.get(i));            
        }
         System.out.println("");
         }
    }
    
}
